﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Windows.Controls;
using System.Xml.Linq;

namespace DataVisualizationDemos
{
    public partial class TreeMapIntroduction : UserControl
    {
        public TreeMapIntroduction()
        {
            InitializeComponent();

            IEnumerable<BlogPost> blogPosts;
            using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("DataVisualizationDemosSL.Data.BlogPostData.xml"))
            {
                var doc = XDocument.Load(stream);
                blogPosts = doc
                    .Element("Posts")
                    .Elements("Post")
                    .Select(p => new BlogPost
                    {
                        Title = p.Element("Title").Value,
                        Date = DateTime.Parse(p.Element("Date").Value, CultureInfo.InvariantCulture),
                        Tags = p.Element("Tags").Elements("Tag").Select(t => t.Value),
                        Length = int.Parse(p.Element("Length").Value, CultureInfo.InvariantCulture),
                        Popularity = double.Parse(p.Element("Popularity").Value, CultureInfo.InvariantCulture),
                    });
            }

            var blogPostsByTag = blogPosts
                .SelectMany(p => p.Tags.Select(t => new { Tag = t, Post = p }))
                .GroupBy(p => p.Tag)
                .Select(g => new BlogTag { Tag = g.Key, Posts = g.Select(p => p.Post).ToArray() });

            AllPosts.DataContext = blogPosts;
            RecentPosts.DataContext = blogPosts.Reverse().Take(10).Reverse();
            ByTag.DataContext = blogPostsByTag;
            ByTagDetailed.DataContext = blogPostsByTag;
        }
    }

    public class DockPanel : System.Windows.Controls.DockPanel
    {
    }
}
